package org.example.questionnairebackend.service.Impl;

import org.apache.catalina.User;
import org.example.questionnairebackend.Data.Users;
import org.example.questionnairebackend.Dto.LoginDto;
import org.example.questionnairebackend.mapper.UsersMapper;
import org.example.questionnairebackend.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public void addUser(Users user) {
        Users user1 = new Users();
        BeanUtils.copyProperties(user, user1);
        user1.setCreatedAt(new Date());
        usersMapper.insert(user1);
    }

    @Override
    public Users login(LoginDto dto) {
        Users user = usersMapper.login(dto);
        if (user != null) {
            return user;
        } else {
            throw new RuntimeException("登录失败,账号或密码不存在");
        }
    }

    @Transactional
    @Override
    public void deleteUser(Long userId) {
        usersMapper.deleteByPrimaryKey(userId);
    }
}
